#pragma once

#include "NRtti.h"

#ifdef WIN32
#include <Windows.h>
#include <process.h>
#endif

#define MS_VC_EXCEPTION 0x406d1388

namespace Core
{
class NThread : public NRtti
{
	RTTI_DECLARATIONS(NThread, Core::NRtti)
public:
	NThread(void);
	virtual ~NThread(void);
	virtual void Run() = 0;
};

#ifdef WIN32

#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
    DWORD dwType;        // must be 0x1000
    LPCSTR szName;       // pointer to name (in same addr space)
    DWORD dwThreadID;    // thread ID (-1 caller thread)
    DWORD dwFlags;       // reserved for future use, most be zero
} THREADNAME_INFO;
#pragma pack(pop)

inline void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
#ifdef _DEBUG
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = szThreadName;
    info.dwThreadID = dwThreadID;
    info.dwFlags = 0;

    __try
    {
        RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (DWORD *)&info);
    }
    __except (EXCEPTION_CONTINUE_EXECUTION)
    {
    }
#else
    dwThreadID;
    szThreadName;
#endif
}
#endif
}